﻿namespace DotNetUtility.Parser
{
    using System;
    /// <summary>
    /// This class is to parse string into it's equivalen value types otherwise it will return the default
    /// value of the related type. NumericParser will parse following value types,
    /// + Numeric types
    ///     |_ Integral types
    ///         |_ sbyte
    ///         |_ byte
    ///         |_ char
    ///         |_ short
    ///         |_ ushort
    ///         |_ int
    ///         |_ uint
    ///         |_ long
    ///         |_ ulong
    ///     |_ Floating-point types
    ///         |_ float
    ///         |_ double
    ///     |_ decimal
    ///     |_ bool
    ///     
    ///     Value types Ref: http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx
    /// </summary>
    public static class NumericParser
    {
        /// <summary>
        /// To parse into int
        /// </summary>
        /// <param name="textToConvert">Text to parse</param>
        /// <returns>Int</returns>
        /// Contributed by Mohammad Rahman        
        public static int ParseToInt32(string textToConvert)
        {
            int result = default(int);
            return Int32.TryParse(textToConvert, out result) ? result : result;
        }

        /// <summary>
        /// To parse into long
        /// </summary>
        /// <param name="textToConvert">Text to parse</param>
        /// <returns>long</returns>
        /// Contributed by Mohammad Rahman        
        public static long ParseToInt64(string textToConvert)
        {
            long result = default(long);
            return Int64.TryParse(textToConvert, out result) ? result : result;
        }

        /// <summary>
        /// To parse into decimal
        /// </summary>
        /// <param name="textToConvert">Text to parse</param>
        /// <returns>decimal</returns>
        /// Contributed by Mohammad Rahman        
        public static decimal ParseToDecimal(string textToConvert)
        {
            decimal result = default(decimal);
            return Decimal.TryParse(textToConvert, out result) ? result : result;
        }

        /// <summary>
        /// To parse into byte
        /// </summary>
        /// <param name="textToConvert">Text to parse</param>
        /// <returns>byte</returns>
        /// Contributed by Mohammad Rahman        
        public static byte ParseToByte(string textToConvert)
        {
            byte result = default(byte);
            return byte.TryParse(textToConvert, out result) ? result : result;
        }

        /// <summary>
        /// To parse into single
        /// </summary>
        /// <param name="textToConvert">Text to parse</param>
        /// <returns>Single</returns>
        /// Contributed by Mohammad Rahman        
        public static Single ParseToSingle(string textToConvert)
        {
            Single result = default(Single);
            return Single.TryParse(textToConvert, out result) ? result : result;
        }

        /// <summary>
        /// To parse into double
        /// </summary>
        /// <param name="textToConvert">Text to parse</param>
        /// <returns>double</returns>
        /// Contributed by Mohammad Rahman
        public static double ParseToDouble(string textToConvert)
        {
            double result = default(double);
            return double.TryParse(textToConvert, out result) ? result : result;
        }
    }
}
